home *** CD-ROM | disk | FTP | other *** search
/ Apple WWDC 1996 / WWDC96_1996 (CD).toast / Technology Materials / QuickTime VR / MacOS / QuickDraw™ 3D 1.0.6F4 SDK / Samples / SampleCode / GeometryTest / GTShell.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-02-20  |  12.4 KB  |  517 lines  |  [TEXT/MPCC]

  1. // Metafile read - shows how to read metafile data.
  2. //
  3. // Nick Thompson - January 6th 1994
  4. //
  5. // © 1995, Apple Computer Inc., All Rights Reserved
  6.  
  7. // system headers
  8. #include <Desk.h>
  9. #include <Dialogs.h>
  10. #include <DiskInit.h>
  11. #include <Fonts.h>
  12. #include <Menus.h>
  13. #include <PictUtil.h>
  14. #include <QDOffScreen.h>
  15. #include <QuickDraw.h>
  16. #include <SegLoad.h>
  17. #include <StandardFile.h>
  18. #include <TextEdit.h>
  19. #include <ToolUtils.h>
  20.  
  21. // for QuickDraw 3D
  22. #include "QD3D.h"
  23. #include "QD3DMath.h"
  24. #include "QD3DDrawContext.h"
  25. #include "QD3DShader.h"
  26. #include "QD3DTransform.h"
  27. #include "QD3DGroup.h"
  28. #include "QD3DCamera.h"
  29.  
  30. #include "GTFile.h"
  31. #include "GTShell.h"
  32. #include "GTSupport.h"
  33.  
  34. #include "Geometries.h"
  35. #include "MyErrorHandler.h"
  36.  
  37. //-------------------------------------------------------------------------------------------
  38. // function prototypes
  39.  
  40. static Boolean        IsAppWindow( WindowPtr window );
  41. static Boolean         IsDAWindow( WindowPtr window ) ;
  42. static Boolean         IsDialogWindow( WindowPtr window );
  43. static void         AdjustMenus( void ) ;
  44. static void         InitToolbox( void ) ;
  45. static void            HandleMenuCommand(long    menuResult) ;
  46. static void         MainEventLoop( void ) ;
  47. static void         HandleKeyPress( EventRecord *event ) ;
  48. //static void         HandleOSEvent(EventRecord *event) ;
  49. static TQ3Status     DocumentDraw3DData( DocumentHdl theDocument ) ;
  50.  
  51. //-------------------------------------------------------------------------------------------
  52. //
  53.  
  54. Boolean         gQuitFlag         = false ;
  55. CursHandle        gWatchCursor ;
  56.  
  57. //-------------------------------------------------------------------------------------------
  58. // main()
  59. // entry point for the application, initialize the toolbox, initialize QuickDraw 3D
  60. // and enter the main event loop.  On exit from the main event loop, we want to call
  61. // the QuickDraw 3D exit function to clean up QuickDraw 3d.
  62.  
  63. void main(void)
  64. {
  65.     TQ3Status    myStatus;
  66.     Rect        rBounds = { 50, 50, 350, 350 } ;
  67.     Str255        title = "\pSpinning Box" ;
  68.  
  69.     InitToolbox() ;
  70.     
  71.     SetCursor(*(GetCursor(watchCursor)));
  72.     
  73.     //    Initialize QuickDraw 3D, open a connection to the QuickDraw 3D library
  74.     myStatus = Q3Initialize();
  75.     if ( myStatus == kQ3Failure ) {
  76.         DebugStr("\pErInitialize returned failure.");    
  77.         ExitToShell() ;
  78.     }
  79.                     
  80.     // install the error handler - this gets called whenever
  81.     // an error occurs, which means we don't have to check so 
  82.     // much
  83.     Q3Error_Register( MyErrorHandler, 0L );        
  84.     Q3Warning_Register( MyWarningHandler, 0L );        
  85.  
  86.     // set up our globals
  87.     gQuitFlag = false ;
  88.     
  89.     // Fake an New command so we have some initial data to work with
  90.     HandleNewCommand() ;
  91.  
  92.     SetCursor(&qd.arrow) ;
  93.  
  94.     // loop till we doop
  95.     MainEventLoop();
  96.     
  97.     //    Close our connection to the QuickDraw 3D library
  98.     myStatus = Q3Exit();
  99.     if ( myStatus == kQ3Failure )
  100.         DebugStr("\pErExit returned failure.");
  101. }    
  102.  
  103.  
  104. //-----------------------------------------------------------------------------
  105. // assumes the port is set up before being called
  106.  
  107. TQ3Status DocumentDraw3DData( DocumentHdl theDocument )
  108. {
  109.     TQ3Status theStatus ;
  110.         
  111.     if((**theDocument).fModel != nil ) {
  112.         HLock((Handle)theDocument );
  113.         Q3View_StartRendering((**theDocument).fView) ;
  114.         do {
  115.             theStatus = SubmitScene( theDocument ) ;
  116.         } while (Q3View_EndRendering((**theDocument).fView) == kQ3ViewStatusRetraverse );
  117.     
  118.         HUnlock((Handle)theDocument );
  119.     }    
  120.     return theStatus ;    
  121.  
  122.     
  123. }
  124.  
  125.  
  126. //----------------------------------------------------------------------------------
  127.  
  128. //-------------------------------------------------------------------------------------------
  129. //
  130.  
  131. short HiWrd(long aLong)
  132. {
  133.     return    (((aLong) >> 16) & 0xFFFF) ;
  134. }
  135.  
  136. //-------------------------------------------------------------------------------------------
  137. //
  138.  
  139. short LoWrd(long aLong)
  140. {
  141.     return    ((aLong) & 0xFFFF) ;
  142.  
  143. }
  144. //-------------------------------------------------------------------------------------------
  145. //    IsAppWindow
  146. //
  147. //    Check to see if a window belongs to the application. If the window pointer
  148. //    passed was NIL, then it could not be an application window. WindowKinds
  149. //    that are negative belong to the system and windowKinds less than userKind
  150. //    are reserved by Apple except for windowKinds equal to dialogKind, which
  151. //    mean it is a dialog.
  152.  
  153. Boolean IsAppWindow(WindowPtr window)
  154. {
  155.     short        windowKind;
  156.  
  157.     if ( window == nil )
  158.         return false;
  159.     else {
  160.         windowKind = ((WindowPeek) window)->windowKind;
  161.         return ((windowKind >= userKind) || (windowKind == dialogKind));
  162.     }
  163. }
  164.  
  165.  
  166. //-------------------------------------------------------------------------------------------
  167. //    IsDAWindow
  168. //
  169. //    Check to see if a window belongs to a desk accessory. It belongs to a DA
  170. //    if the windowKind field of the window record is negative.
  171.  
  172. Boolean IsDAWindow(WindowPtr window)
  173. {
  174.     if ( window == nil )
  175.         return false;
  176.     else
  177.         return ( ((WindowPeek) window)->windowKind < 0 );
  178. }
  179.  
  180.  
  181. //-------------------------------------------------------------------------------------------
  182. //    IsDialogWindow
  183. //
  184. //    Check to see if a window is a dialog window. We can determine this be
  185. //    checking to see if the windowKind field is equal to dialogKind.
  186.  
  187. Boolean IsDialogWindow(WindowPtr window)
  188. {
  189.     if ( window == nil )
  190.         return false;
  191.     else
  192.         return ( ((WindowPeek) window)->windowKind == dialogKind );
  193. }
  194.  
  195.  
  196. //-------------------------------------------------------------------------------------------
  197. //
  198. void AdjustMenus()
  199. {
  200.     WindowPtr    window;
  201.     MenuHandle    menu;
  202.  
  203.     window = FrontWindow();
  204.  
  205.     menu = GetMHandle(mFile);
  206.     
  207.     if ( window != nil )
  208.         EnableItem(menu, iClose);
  209.     else
  210.         DisableItem(menu, iClose);
  211.  
  212.     menu = GetMHandle(mEdit);
  213.     if ( IsDAWindow(window) ) {
  214.         EnableItem(menu, iUndo);
  215.         EnableItem(menu, iCut);
  216.         EnableItem(menu, iCopy);
  217.         EnableItem(menu, iClear);
  218.         EnableItem(menu, iPaste);
  219.     } else {                        
  220.         DisableItem(menu, iUndo);
  221.         DisableItem(menu, iCut);
  222.         DisableItem(menu, iCopy);
  223.         DisableItem(menu, iClear);
  224.         DisableItem(menu, iPaste);
  225.     }
  226. }
  227.  
  228. //-------------------------------------------------------------------------------------------
  229. //
  230. void InitToolbox()
  231. {
  232.     Handle        menuBar;
  233.     EventRecord event;
  234.     short        count;
  235.  
  236.     // initialize application globals
  237.     gQuitFlag = false;
  238.     gWatchCursor = GetCursor( watchCursor ) ;
  239.     SetCursor( *gWatchCursor ) ;
  240.     
  241.     MaxApplZone() ;
  242.     MoreMasters() ; MoreMasters() ; MoreMasters() ; 
  243.     
  244.     // Init the managers
  245.     InitGraf( &qd.thePort );
  246.     InitFonts();
  247.     InitWindows();
  248.     InitMenus();
  249.     TEInit();
  250.     InitDialogs(nil);
  251.  
  252.  
  253.     FlushEvents( everyEvent, 0 ) ;
  254.         
  255.     //    This next bit of code waits until the Finder brings our application
  256.     //    to the front. This gives us a better effect if we open a window at
  257.     //    startup.
  258.  
  259.     for (count = 1; count <= 3; ++count)
  260.         EventAvail(everyEvent, &event);
  261.  
  262.     menuBar = GetNewMBar( 128 );
  263.     if ( menuBar == nil )
  264.          ExitToShell() ;
  265.          
  266.     SetMenuBar( menuBar );
  267.     DisposHandle( menuBar );
  268.     AddResMenu( GetMHandle(mApple), 'DRVR' );    // Add DA names to Apple menu
  269.     AdjustMenus();
  270.     DrawMenuBar();
  271.     InitCursor();                                // reset the cursor
  272. }
  273.  
  274. //-------------------------------------------------------------------------------------------
  275. //    HandleMenuCommand
  276. //
  277. //    This is called when an item is chosen from the menu bar (after calling
  278. //    MenuSelect or MenuKey). It performs the right operation for each command.
  279. //    It is good to have both the result of MenuSelect and MenuKey go to one
  280. //    routine like this to keep everything organized.
  281.  
  282. void HandleMenuCommand(long    menuResult)
  283. {
  284.     short        menuID;                // Resource ID# of the selected menu
  285.     short        menuItem;            // Item number of the selected menu
  286.     Str255        daName;
  287.     DialogPtr    theDialog ; 
  288.     short        itemHit ;
  289.     WindowPtr    theWindow ;
  290.  
  291.     menuID = HiWrd(menuResult);
  292.     menuItem = LoWrd(menuResult);
  293.     switch ( menuID ) {
  294.         case mApple:
  295.             switch ( menuItem ) {
  296.                 ModalFilterUPP         theProc ;
  297.  
  298.                 case iAbout:
  299.                 
  300.                     theDialog = GetNewDialog ( 128, nil, (WindowPtr)-1 );
  301.                     
  302.                     // these two lil' snappers are system 7 only
  303.                     // so if you use them, check before!!
  304.                     GetStdFilterProc( &theProc ) ;
  305.                     SetDialogDefaultItem(theDialog, ok) ;
  306.                     
  307.                     
  308.                     do {
  309.                         ModalDialog ( theProc, &itemHit );
  310.                     } while( itemHit != ok ) ;
  311.                     DisposDialog ( theDialog );
  312.                     break;
  313.  
  314.                 default:            // All non-About items in this menu are DAs
  315.                     GetItem(GetMHandle(mApple), menuItem, daName);
  316.                     (void) OpenDeskAcc(daName);
  317.                     break;
  318.             }
  319.             break;
  320.         case mFile:
  321.             switch ( menuItem ) {
  322.                 case iNew:
  323.                     HandleNewCommand() ;
  324.                     break;
  325.                 case iOpen:
  326.                     HandleOpenCommand() ;
  327.                     break;
  328.                 case iClose:
  329.                     HandleCloseWindow( FrontWindow() ) ;
  330.                     break;
  331.                 case iQuit:
  332.                     gQuitFlag = true;
  333.                     break;
  334.             }
  335.             break;
  336.         case mEdit:
  337.             switch (menuItem) {
  338.                 // Call SystemEdit for DA editing & MultiFinder 
  339.                 // since we don’t do any Editing yet
  340.                 case iUndo:
  341.                 case iCut:
  342.                 case iCopy:
  343.                 case iPaste:
  344.                 case iClear:
  345.                     (void) SystemEdit(menuItem-1);
  346.                     break;
  347.             }
  348.             break;
  349.         case mGeometry:
  350.             theWindow = FrontWindow() ;
  351.             if( theWindow ) {
  352.                 DocumentHdl theDocument = (DocumentHdl) GetWRefCon( theWindow ) ;
  353.                 if( theDocument != nil ) {
  354.                     TQ3Point3D        myOrigin = { 0, 0, 0 } ;
  355.                 
  356.                     HLock( (Handle)theDocument ) ;
  357.                     
  358.                     if((**theDocument).fModel != nil) {
  359.                         Q3Object_Dispose((**theDocument).fModel) ;                
  360.                         (**theDocument).fModel = nil ;
  361.                     }
  362.                     
  363.                     (**theDocument).fModel = BuildGeometry(menuItem) ;
  364.                     
  365.                     (**theDocument).fGroupScale = 1;                
  366.                     (**theDocument).fGroupCenter = myOrigin ;            
  367.                     AdjustCamera(    theDocument,
  368.                                     (theWindow->portRect.right - theWindow->portRect.left),
  369.                                     (theWindow->portRect.bottom - theWindow->portRect.top) ) ;
  370.                                     
  371.                     HUnlock((Handle)theDocument);
  372.                 }
  373.             }
  374.             SetPort( (GrafPtr)theWindow ) ;
  375.             InvalRect( & theWindow->portRect ) ;
  376.             
  377.             break ;
  378.     }
  379.     HiliteMenu(0);        // Unhighlight what MenuSelect or MenuKey hilited
  380. }
  381.  
  382. //-------------------------------------------------------------------------------------------
  383. //
  384. void MainEventLoop()
  385. {
  386.     EventRecord     event;
  387.     WindowPtr       window;
  388.     short           thePart;
  389.     Rect            screenRect, updateRect;
  390.     Point            aPoint = {100, 100};
  391.     CGrafPtr        savedPort ;
  392.     
  393.     DocumentHdl        theDocument ;
  394.     
  395.  
  396.     while( !gQuitFlag )
  397.     {
  398.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  399.         {
  400.  
  401.             switch (event.what) {
  402.                 case mouseDown:
  403.                 
  404.                     thePart = FindWindow( event.where, &window );
  405.                     
  406.                     switch( thePart ) {
  407.                         case inMenuBar: 
  408.                             AdjustMenus();
  409.                             HandleMenuCommand(MenuSelect(event.where));
  410.                             break;
  411.                         
  412.                         case inDrag:
  413.                     
  414.                             screenRect = (**GetGrayRgn()).rgnBBox;
  415.                             DragWindow( window, event.where, &screenRect );
  416.                             break ;
  417.                     
  418.                         case inContent:
  419.                     
  420.                             if (window != FrontWindow())
  421.                                 SelectWindow( window );
  422.                             break ;
  423.                     
  424.                         case inGoAway:
  425.                             if (TrackGoAway( window, event.where )) {
  426.                                 HandleCloseWindow( window ) ;
  427.                             }
  428.                             break ;
  429.                             
  430.                         default:
  431.                             break ;
  432.                     }
  433.                     break ;
  434.                             
  435.                         
  436.                 case updateEvt:
  437.                 
  438.                     window = (WindowPtr)event.message;
  439.                     theDocument = (DocumentHdl) GetWRefCon( window ) ;
  440.                     
  441.                     if( theDocument != nil ) {
  442.                         updateRect = (**(window->visRgn)).rgnBBox;
  443.                         
  444.                         GetPort( (GrafPtr *)&savedPort) ;
  445.                         SetPort( window ) ;
  446.                         BeginUpdate( window );
  447.                         DocumentDraw3DData( theDocument ) ;
  448.                         EndUpdate( window );
  449.                         SetPort( (GrafPtr)savedPort ) ;
  450.                     }
  451.  
  452.                     break ;
  453.                     
  454.                 case keyDown:
  455.                 case autoKey:
  456.                     HandleKeyPress(&event);
  457.                     break;
  458.                     
  459.                 case diskEvt:
  460.                     if ( HiWrd(event.message) != noErr ) 
  461.                         (void) DIBadMount(aPoint, event.message);
  462.                     break;
  463.                     
  464.                 case osEvt:
  465.                 case activateEvt:
  466.                     break;
  467.  
  468.  
  469.             }
  470.         }
  471.         else {
  472.             WindowPtr    theWindow ;
  473.             
  474.             theWindow = FrontWindow() ;
  475.             if( theWindow ) {
  476.                 theDocument = (DocumentHdl) GetWRefCon( theWindow ) ;
  477.                 if( theDocument != nil ) {
  478.  
  479.                     // we received a null event, rotate the object
  480.                     TQ3Matrix4x4    tmp;
  481.                     Rect        theRect = ((GrafPtr)theWindow)->portRect ;
  482.                     
  483.                     SetPort((GrafPtr)theWindow) ;
  484.                     Q3Matrix4x4_SetRotate_XYZ(&tmp, 0.1, 0.12, 0.08);
  485.                     HLock( (Handle)theDocument ) ;
  486.                     Q3Matrix4x4_Multiply(&(**theDocument).fRotation, &tmp, &(**theDocument).fRotation);
  487.                     HUnlock( (Handle)theDocument ) ;
  488.         
  489.                     InvalRect( &theRect ) ;
  490.                 }
  491.             }
  492.         }
  493.     }
  494. }
  495.  
  496.  
  497. //-------------------------------------------------------------------------------------------
  498. //
  499. void HandleKeyPress(EventRecord *event)
  500. {
  501.     char    key;
  502.  
  503.     key = event->message & charCodeMask;
  504.     if ( event->modifiers & cmdKey ) {        /* Command key down? */
  505.         AdjustMenus();                        /* Enable/disable/check menu items properly */
  506.         HandleMenuCommand(MenuKey(key));
  507.     } else {
  508.         /* DoKeyPress(event) */;
  509.     }
  510. }
  511.  
  512. //-------------------------------------------------------------------------------------------
  513. //
  514.  
  515.  
  516.  
  517.